'use strict';
require('./index.css')
require('page/common/nav/index.js')
require('page/common/header/index.js')
var _mm = require('util/mm.js')
var _product = require('service/product-service.js')
var template = require('./index.string')
var Pagination = require('util/pagination/index.js')

var page = {
    data : {
        listParam : {
            keyword     : _mm.getUrlParam('keyword') || '',
            categoryId  : _mm.getUrlParam('categoryId') || '',
            orderBy     : _mm.getUrlParam('orderBy') || 'default',     //排序方法
            pageNum     : _mm.getUrlParam('pageNum') || 1,      //跳到第几页
            pageSize    : _mm.getUrlParam('pageSize') || 15      //每页显示的数据
        }
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadList()
    },
    
    bindEvent : function(){
        var _this = this
      $('.sort-item').click(function(){
          //点击排序 页码回到第一页
          _this.data.listParam.pageNum = 1 ;
          var $this = $(this)
        //   点击默认排序
        if($this.data('type') === "default"){
            if($this.hasClass('active')){
                //已经是active样式
                return
            }else{
                $this.addClass('active').siblings('.sort-item').removeClass('active asc desc');
                _this.data.listParam.orderBy = 'default'
            }
        }
        else if($this.data('type') === 'price'){
            //证明价格排序
            $this.addClass('active').siblings('.sort-item').removeClass('active asc desc');
            // 默认是升序排列
            if(!$this.hasClass('asc')){
                $this.addClass('asc').removeClass('desc')
                _this.data.listParam.orderBy = 'price_asc'
            }else{
                $this.addClass('desc').removeClass('asc')
                _this.data.listParam.orderBy = 'price_desc'
            }
        }
        // 重新加载列表 发送数据
           _this.loadList() 
      })
    },
    //加载list
    loadList : function(){
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.p-list-con');
            $pListCon.html('<div class="loading"></div>')
            // 删除参数中不必要的字段
            listParam.categoryId ? (delete listParam.keyword ) : ( delete listParam.categoryId );
            // 请求接口
        _product.getProductList(listParam,function(res){
                listHtml = _mm.renderHtml(template,{ 
                    list : res.list
                })
                $pListCon.html(listHtml)
                // 加载分页信息
                _this.loadPagination({
                    hasPreviousPage : res.hasPreviousPage,
                    prePage         : res.prePage,
                    hasNextPage     : res.hasNextPage,
                    nextPage        : res.nextPage,
                    pageNum         : res.pageNum, //第几页
                    pages           : res.pages   //总页数
                })
        },function(errMsg){
                 _mm.errorTips(errMsg)
        });

    },
    //加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        // this.pagination ? '' : (this.pagination = new Pagination())
        var pagination = new Pagination()
        pagination.render($.extend({},pageInfo,{
            container : $('.pagination'),  //放置分页的容器
            onSelectPage : function(pageNum){
                // this.data.listParam.pageNum 变了 重新发送数据请求   也可单独写一个点击出来
                _this.data.listParam.pageNum = pageNum
                _this.loadList()
            }
        }))
    }
}

$(function(){
    page.init()
})

